# -*- coding: utf-8 -*-
from xTkinter import * 
import tkFileDialog
import tkMessageBox as msgBox
import sys
from Validation_Contraintes import *
Validateur = ValidationContrainte()
import xmlrpclib
class Vue_ReglesAffaires(Frame):
    def __init__(self,master,parent,**cnf):
        self.ListeContraintes = []
        self.ListLabels = []
        self.ListeBoutons = []
        self.parent = parent
        self.master=master
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=800,height=600)
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(135,0)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirContraintes)
        self.CBTables.setText('<Choisir>')
        self.ID_Contraintes = self.parent.serveur.ExecuterRequeteSQLWhere("select perp_contraintes_id.nextval from dual")[1][0][0]
        
    
    def obtenirTables(self):
        Reponse = self.parent.serveur.ObtenirListeTables(self.parent.user,self.parent.profil)
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i][0])
                self.CBTables.setText(self.ListeChamps[i][0])
        
    def obtenirContraintes(self):
        self.ListeContraintes = []
        Reponse = self.parent.serveur.ObtenirContraintes(self.CBTables.getText())
        for Label in self.ListLabels:
            Label.destroy()
        for Bouton in self.ListeBoutons:
            Bouton.destroy()
        if(Reponse[0]==1):
            self.ListeContraintes = Reponse[1]
            Position = 0
            self.ListeBoutons.append(xButton(self,text="Ajouter Contrainte",width=16, command = self.AjouterContrainte ))
            self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(0,(Position+1)*25+5)
            Position = 1
            for Contrainte in self.ListeContraintes:
                pass
                try:
                    texte = Contrainte['MESSAGE_GUIDE'] 
                    texte = texte + '(' + Contrainte['OPERANDE_GAUCHE'] + '.'
                    texte = texte + Contrainte['ATTRIBUT_GAUCHE']
                    texte = texte + Contrainte['COMPARAISON']
                    texte = texte + Contrainte['OPERANDE_DROITE'] + ')'
                    self.ListLabels.append(xLabel(self,text=texte, width=len(texte)+2, anchor=SW ))
                    self.ListLabels[len(self.ListLabels)-1].setLocation(0,(Position+1)*25+5)
                except:
                    texte = "Contrainte invalide, corrigez-la"
                    self.ListLabels.append(xLabel(self,text=texte, width=len(texte)+2, anchor=SW ))
                    self.ListLabels[len(self.ListLabels)-1].setLocation(0,(Position+1)*25+5)

                self.ListeBoutons.append(xButton(self,text="Editer",width=12, command = lambda contrainte=Contrainte : self.OuvrirFenetreEdition(contrainte)))
                self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(500,(Position+1)*25+5)
                self.ListeBoutons.append(xButton(self,text="Supprimer",width=12, command = lambda contrainte=Contrainte : self.SupprimerContrainte(contrainte)))
                self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(600,(Position+1)*25+5)
                Position = Position + 1
                                 
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
        
    def OuvrirFenetreEdition(self, Contrainte):
        root=Toplevel()
        root.geometry('850x500')
        print Contrainte
        vi=Vue_EditionReglesAffaires(Contrainte, root,self)
        root.mainloop()
    
    
    
    def MettreAJourContrainte(self, Contrainte):
        
        if Contrainte.has_key('ID_CONTRAINTES'):
            Contrainte['OPERANDE_GAUCHE'] = self.CBTables.getText()
            print "Mise a jour" ,Contrainte
            where = " where ID_CONTRAINTES = " + Contrainte['ID_CONTRAINTES']
            self.parent.serveur.MettreAJour('PERP_CONTRAINTES', Contrainte, where)
        else:
            Contrainte['OPERANDE_GAUCHE'] = self.CBTables.getText()
            Contrainte['ID_CONTRAINTES'] = str(self.ID_Contraintes)
            print self.ID_Contraintes
            self.ID_Contraintes = self.ID_Contraintes + 1
            print " where ajouter" ,Contrainte
            self.parent.serveur.InsererEnregistrement('PERP_CONTRAINTES', Contrainte)
        self.obtenirContraintes()
    
    def SupprimerContrainte(self, Contrainte):
        print "a effacer " , Contrainte
<<<<<<< .mine
        where = " where ID_CONTRAINTES = " + Contrainte['ID_CONTRAINTES']
        self.parent.serveur.EffacerEnregistrement('PERP_CONTRAINTES', where)
        self.parent.serveur.ExecuterRequeteSQL("commit")
        self.obtenirContraintes()
          
=======
        where = " where 'ID_CONTRAINTES' = " + Contrainte['ID_CONTRAINTES']
        self.parent.serveur.EffacerEnregistrement('PERP_CONTRAINTES', where)
        self.obtenirContraintes()     
>>>>>>> .r406
        
    
    def AjouterContrainte(self):
        root=Toplevel()
        root.geometry('850x500')
        vi=Vue_EditionReglesAffaires(None, root,self)
        root.mainloop()
        
class Vue_EditionReglesAffaires(Frame):
    def __init__(self,Contrainte, master,parent,**cnf):
        KeysList=cnf.keys()
        self.parent = parent
        self.master=master
        self.width=400
        self.height=200
        if ('width' in KeysList):
            self.width=cnf['width']
        if ('height' in KeysList):
            self.height=cnf['height']
        Frame.__init__(self,master,cnf)        
        self.config(bg='Floralwhite')#(bg='#6B979C',relief='raised',bd=3)
        self.pack(side=LEFT, anchor=NW)
        self.configure(width=850,height=500)
        self.Contrainte = Contrainte
        
    
        xButton(self,text="Fermer",width=12, command = self.Quitter ).setLocation(16,0)
                
        
        self.Attribut1_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut1_Label.place(x=310,y=150)
        self.Attribut_Operande_Gauche=xCombobox(self,width=20)
        self.Attribut_Operande_Gauche.place(x=310,y=175)
        
        self.Comparaison_Label=xLabel(self,text='Comparaison',height=1,width=10)
        self.Comparaison_Label.place(x=460,y=150)
        self.Comparaison_Combo_Box=xCombobox(self)
        self.Comparaison_Combo_Box.width=10
        self.Comparaison_Combo_Box.setListWidth(25)
        self.Comparaison_Combo_Box.setListHeight(5)
        self.Comparaison_Combo_Box.place(x=460,y=175)
        
        self.Table_Operande_Droite=xCombobox(self,width=20)
        self.Table_Operande_Droite.place(x=550,y=175)
        self.Table2_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Table2_Label.place(x=550,y=150)
        
        self.Attribut2_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut2_Label.place(x=695,y=150)
        self.Attribut_Operande_Droite=xCombobox(self,width=20)
        self.Attribut_Operande_Droite.place(x=695,y=175)
        
        self.Message_Erreur_Text=xEntry(self,text='Message d''erreur',width=60)
        self.Message_Erreur_Text.place(x=255,y=365)
        self.Message_Erreur_Label=xLabel(self,text='Message d''erreur',height=1,width=15)
        self.Message_Erreur_Label.place(x=125,y=365)
                
        self.Conseils_Label=xLabel(self,text='Conseils',height=1,width=10)
        self.Conseils_Label.place(x=185,y=405)
        self.Conseils_Text=xEntry(self,text='Conseils',width=60)
        self.Conseils_Text.place(x=255,y=405)
        

        self.AssociationColonneChamps = {
                                        "ATTRIBUT_GAUCHE":self.Attribut_Operande_Gauche,
                                        "COMPARAISON":self.Comparaison_Combo_Box,
                                        "OPERANDE_DROITE":self.Table_Operande_Droite,
                                        "ATTRIBUT_DROITE":self.Attribut_Operande_Droite,
                                        "MESSAGE_ERREUR":self.Message_Erreur_Text,
                                        "MESSAGE_GUIDE":self.Conseils_Text,
                                        "ATTRIBUT_GAUCHE":self.Attribut_Operande_Gauche
                                        }
        self.initialiserComboBox()
        self.RemplirChamp(self.Contrainte)
        pass

    def Quitter(self):
        #if MsgBox.askokcancel("Attention", "Vous annulez l'ouverture de session."):
        self.destroy()
        self.master.destroy()
        Contrainte = {}
        for champs in self.AssociationColonneChamps.keys():
            if self.AssociationColonneChamps[champs] <> None:
                Contrainte[champs] = self.AssociationColonneChamps[champs].getText()
        if self.Contrainte <> None:
            Contrainte['ID_CONTRAINTES'] = self.Contrainte['ID_CONTRAINTES']
        self.parent.MettreAJourContrainte(Contrainte)
        pass
    
    def close(self):
        self.destroy()
        pass
    
    def initialiserComboBox(self):
        #operande
        for Operande in Validateur.getOperande():
            self.Comparaison_Combo_Box.addItem(Operande)
                
    def RemplirChamp(self, Contrainte):
        print Contrainte
        if self.Contrainte == None:
            for Champs in self.AssociationColonneChamps.keys():
                if self.AssociationColonneChamps[Champs] <> None:
                    self.AssociationColonneChamps[Champs].setText('')
        else:
            for Champs in self.Contrainte:
                if self.AssociationColonneChamps.has_key(Champs):
                    if self.AssociationColonneChamps[Champs] <> None:
                        self.AssociationColonneChamps[Champs].setText(Contrainte[Champs])
        pass   

            



    
    
    
